package com.jinwei.qunguanjia.web.system.controller;

import com.jinwei.qunguanjia.common.annotation.Log;
import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.enums.BusinessTypeEnum;
import com.jinwei.qunguanjia.common.utils.JsonUtils;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.poi.ExcelUtil;
import com.jinwei.qunguanjia.system.dto.SysDictDataDTO;
import com.jinwei.qunguanjia.system.service.SysDictDataService;
import com.jinwei.qunguanjia.web.system.vo.SysDictDataVO;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static com.jinwei.qunguanjia.common.utils.PageUtils.startPage;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController {

    @Resource
    private SysDictDataService sysDictDataService;

    /**
     * 新增字典类型
     * @param sysDictDataVO 对象
     */
    @PreAuthorize("@ss.hasPermission('system:dict:save')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/save")
    public Response add(@Validated @RequestBody SysDictDataVO sysDictDataVO) throws IllegalAccessException {
        SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
        BeanUtils.copyProperties(sysDictDataVO, sysDictDataDTO);
        sysDictDataService.save(sysDictDataDTO);
        return Response.success();
    }

    /**
     * 修改保存字典类型
     * @param sysDictDataVO 对象
     */
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/update")
    public Response edit(@Validated @RequestBody SysDictDataVO sysDictDataVO) throws IllegalAccessException {
        SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
        BeanUtils.copyProperties(sysDictDataVO, sysDictDataDTO);
        sysDictDataService.update(sysDictDataDTO);
        return Response.success();
    }

    /**
     * 删除字典类型
     * @param ids id列表
     */
    @PreAuthorize("@ss.hasPermission('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{ids}")
    public Response remove(@PathVariable List<String> ids) {
        sysDictDataService.remove(ids);
        return Response.success();
    }

    @PreAuthorize("@ss.hasPermission('system:dict:list')")
    @GetMapping("/list")
    public PageDataInfo<SysDictDataVO> list(SysDictDataVO sysDictDataVO) throws IllegalAccessException {
        SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
        BeanUtils.copyProperties(sysDictDataVO, sysDictDataDTO);
        startPage();
        PageDataInfo<SysDictDataDTO> pageDataInfo = sysDictDataService.listPage(sysDictDataDTO);
        PageDataUtil<SysDictDataDTO, SysDictDataVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(pageDataInfo, SysDictDataVO.class);
    }

    @Log(title = "字典数据", businessType = BusinessTypeEnum.EXPORT)
    @PreAuthorize("@ss.hasPermission('system:dict:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataVO sysDictDataVO) throws IllegalAccessException {
        SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
        BeanUtils.copyProperties(sysDictDataVO, sysDictDataDTO);
        List<SysDictDataDTO> list = sysDictDataService.list(sysDictDataDTO);
        ExcelUtil<SysDictDataVO> util = new ExcelUtil<>(SysDictDataVO.class);
        util.exportExcel(response, JsonUtils.copyList(list, SysDictDataVO.class), "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    @GetMapping(value = "/{id}")
    public Response<SysDictDataVO> getInfo(@PathVariable String id) {
        SysDictDataVO  sysDictDataVO= new SysDictDataVO();
        BeanUtils.copyProperties(sysDictDataService.get(id), sysDictDataVO);
        return Response.success(sysDictDataVO);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public Response<List<SysDictDataVO>> dictType(@PathVariable String dictType) throws IllegalAccessException {
        return Response.success(JsonUtils.copyList(sysDictDataService.list(SysDictDataDTO.builder().dictType(dictType).build()), SysDictDataVO.class));
    }

}
